#include <cmath>
#include "job.h"

Job::Job(int w, int h, double topLeftX, double topLeftY, double scale, int limit, double *buffer) 
	: 	w_ (w), h_ (h),
		topLeftX_ (topLeftX), topLeftY_ (topLeftY),
		scale_ (scale),
		limit_ (limit),
		buffer_ (buffer)
{
	
}

Job::~Job() {

}

DoubleJob::DoubleJob(int w, int h, double topLeftX, double topLeftY, double scale, int limit, double *buffer) : Job(w, h, topLeftX, topLeftY, scale, limit, buffer) {
	
}


void DoubleJob::execute() {
	int i;
	double zx, zy, zxSq, zySq, cx, cy;
	double escapeRadius = 2;
	for (int y = 0; y < h_; y++) {
		for (int x = 0; x < w_; x++) {
			zx = 0;
			zy = 0;
			zxSq = 0;
			zySq = 0;
			cx = (x * scale_) + topLeftX_;
			cy = (y * scale_) + topLeftY_;
			for (i = 1; i < limit_ && zxSq + zySq < 4; i++) {
				zy = (2 * zx * zy) + cy;
				zx = zxSq - zySq + cx;
				zxSq = zx * zx;
				zySq = zy * zy;
			}
			if (i == limit_)
				buffer_[y * w_ + x] = 1;
			else {
				double foo = log(sqrt(zxSq + zySq)) / log(escapeRadius);
				buffer_[y * w_ + x] = (i - log(foo) / log(2)) / limit_;
			}
		}
	}
}

DoubleJob::~DoubleJob() {
	
}
